<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="test" width="100" height="100"></canvas>

    <button id="download">点击下载</button>
    <button id="switch">switch</button>
    <script>

        function write(idName) {
            const cvs = document.getElementById(idName);
            const ctx = cvs.getContext('2d');
            let isDraw = false;
            cvs.addEventListener('mousedown', (e) => {
                e = e || window.event;
                isDraw = true;
                ctx.moveTo(e.pageX, e.pageY)
            })
            cvs.addEventListener('mousemove', (e) => {
                e = e || window.event;

                if (isDraw) {
                    console.log(e.pageX, e.pageY)
                    ctx.lineTo(e.pageX, e.pageY);
                    ctx.stroke();
                }

            })
            cvs.addEventListener('mouseup', (e) => {
                isDraw = false;
            })
        }
        document.getElementById('download').addEventListener('click', function () {
            downloadCanvas('test')
            blobToBase64(blob).then(res => {
                // 转化后的base64
                console.log('base64', res)
            })
        }
        )

        document.getElementById('switch').addEventListener('click', function () {
            // 只能用于canvas 转化成base64
            var imgData = document.getElementById("test").toDataURL({ format: 'png', quality: 1, width: 20000, height: 4000 });
            // var strDataURI = imgData.substr(22, imgData.length);
            var arr = imgData.split(","),
                mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]),
                n = bstr.length,
                u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            var blob = new Blob([u8arr], { type: mime });
            blobToBase64(blob).then(res => {
                // 转化后的base64
                console.log('base64', res)
            })
        }
        )
        write("test")

        function blobToBase64(blob) {
            return new Promise((resolve, reject) => {
                const fileReader = new FileReader();
                fileReader.onload = (e) => {
                    resolve(e.target.result);
                };
                // readAsDataURL
                fileReader.readAsDataURL(blob);
                fileReader.onerror = () => {
                    reject(new Error('blobToBase64 error'));
                };
            });
        }


        function downloadCanvas(idName) {
            var link = document.createElement("a");
            // 只能用于canvas 转化成base64
            var imgData = document.getElementById(idName).toDataURL({ format: 'png', quality: 1, width: 20000, height: 4000 });
            // var strDataURI = imgData.substr(22, imgData.length);
            var arr = imgData.split(","),
                mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]),
                n = bstr.length,
                u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            var blob = new Blob([u8arr], { type: mime });
            console.log(blob)
            var objurl = URL.createObjectURL(blob);
            link.download = "grid1.png";
            link.href = objurl;
            link.click();
        }


    </script>
</body>

</html>